<template>
  <div class="app-container home">
    <el-row :gutter="20">
      <el-col :sm="24" :lg="12" style="padding-left: 20px">
        <p>
          1.标签分组名称不能重复。<br/>
          2.标签总数量不限制，但给每个客户打标签最多为20个。<br/>
          3.标签分组一旦被删除，归属于该分组的标签均将被删除。<br/>
          4.【未分组】为固定存在，不可修改名称，亦不可删除。<br/>
        </p>
      </el-col>
    </el-row>
    <el-row>
      <el-button icon="el-icon-refresh" size="mini">同步企业微信标签</el-button>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wework:employee:add']"
        >新增标签组</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20" :key="group" v-for="group in tagGroups">
      <el-col :span="2">
        <el-button type="primary" size="small" icon="el-icon-edit">
          {{group.groupName}}
        </el-button>
      </el-col>
      <el-col :span="16">
        <el-badge  class="item" :key="tag" v-for="tag in group.tagList" :value="tag.nums">
          <el-tag size="small" closable @close="handleClose(tag)">{{tag.name}}{{tag.nums}}</el-tag>
        </el-badge>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-col>
    </el-row>

    <div class="line"></div>
  </div>
</template>
<script>
import { listTag, getTag, delTag, addTag, updateTag, exportTag } from "@/api/wework/tag";
import Template from "@/views/multitenant/template";
import {listContact} from "@/api/wework/contact";
export default {
  name: "Tag",
  components: {Template},
  data() {
    return {
      tagGroups: [],
      inputVisible: false,
      inputValue: ''
    };
  },
  created() {
    this.getList();
    this.getDicts("contact_wx_addway").then(response => {
      this.addwayOptions = response.data;
    });
  },

  methods: {
    /** 查询标签组 */
    getList() {
      // this.loading = true;
      listTag(this.queryParams).then(response => {
        this.tagGroups = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>
<style scoped lang="scss">
.home {
  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;
  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }
  .item {
    margin-top: 10px;
    margin-right: 40px;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
